import { authControllerLoginUser } from "@/app/servers/api/auth";
import { NextRequest } from "next/server";

export async function POST(request: NextRequest) {
  const values: { email: string; password: string } = await request?.json();

  try {
    const { data } = await authControllerLoginUser(
      {
        email: values.email,
        password: values.password,
      },
      {
        next: {
          headers: request.headers,
          cookies: request.cookies,
        },
      }
    );
    const maxAge = 24 * 60 * 60 * 1000;
    return new Response(JSON.stringify(data), {
      headers: {
        // 设置过期时间一天
        "Set-Cookie": `token=${data.access_token}; Path=/; HttpOnly; SameSite=Lax; Max-Age=${maxAge}`,
      },
    });
  } catch {
    return new Response(JSON.stringify({ msg: "账号或密码错误" }), {
      status: 400,
    });
  }
}
